package interceptor

import (
	"chain_bridge_service/configs"
	"chain_bridge_service/internal/code"
	"chain_bridge_service/internal/pkg/core"
	"chain_bridge_service/internal/repository/mongodb/user"
	"chain_bridge_service/internal/tools"
	"go.mongodb.org/mongo-driver/mongo"
	"net/http"
)

var whiteListPath = map[string]bool{
	"/login/web": true,
}

func (i *interceptor) CheckSignature(client *mongo.Client) core.HandlerFunc {
	return func(c core.Context) {
		//if !env.Active().IsPro() {
		//	return
		//}

		// 签名信息
		authorization := c.GetHeader(configs.HeaderSignToken)
		if authorization == "" {
			c.AbortWithError(core.Error(
				http.StatusOK,
				code.AuthorizationError,
				code.Text(code.AuthorizationError)),
			)
			return
		}

		token, err := tools.ParseToken(authorization)
		if err != nil {
			c.AbortWithError(core.Error(
				http.StatusOK,
				code.AuthorizationParseToken,
				code.Text(code.AuthorizationParseToken)),
			)
			return
		}
		models := new(user.User)
		models.ApiKey = token
		result, err := models.CheckApikey(client)

		if err != nil {
			c.AbortWithError(core.Error(
				http.StatusOK,
				code.UserNotExist,
				code.Text(code.UserNotExist)),
			)
			return
		}

		//if result.Role != "2" {
		//	c.AbortWithError(core.Error(
		//		http.StatusOK,
		//		code.UserRoleError,
		//		code.Text(code.UserRoleError)),
		//	)
		//	return
		//}

		c.SetInfo("userInfo", result)
		c.Next()
		//err = i.cache.Set("userId", result.Id, 24*time.Hour)
		//if err != nil {
		//	c.AbortWithError(core.Error(
		//		http.StatusBadRequest,
		//		code.AuthorizationError,
		//		code.Text(code.AuthorizationError)).WithError(errors.New("cache set failed")),
		//	)
		//	return
		//}
		//// 时间信息
		//date := c.GetHeader(configs.HeaderSignTokenDate)
		//if date == "" {
		//	c.AbortWithError(core.Error(
		//		http.StatusBadRequest,
		//		code.AuthorizationError,
		//		code.Text(code.AuthorizationError)).WithError(errors.New("Header 中缺少 Date 参数")),
		//	)
		//	return
		//}
		//
		//// 通过签名信息获取 key
		//authorizationSplit := strings.Split(authorization, " ")
		//if len(authorizationSplit) < 2 {
		//	c.AbortWithError(core.Error(
		//		http.StatusBadRequest,
		//		code.AuthorizationError,
		//		code.Text(code.AuthorizationError)).WithError(errors.New("Header 中 Authorization 格式错误")),
		//	)
		//	return
		//}
		//
		//key := authorizationSplit[0]
		//
		//data, err := i.authorizedService.DetailByKey(c, key)
		//if err != nil {
		//	c.AbortWithError(core.Error(
		//		http.StatusBadRequest,
		//		code.AuthorizationError,
		//		code.Text(code.AuthorizationError)).WithError(err),
		//	)
		//	return
		//}
		//
		//if data.IsUsed == authorized.IsUsedNo {
		//	c.AbortWithError(core.Error(
		//		http.StatusBadRequest,
		//		code.AuthorizationError,
		//		code.Text(code.AuthorizationError)).WithError(errors.New(key + " 已被禁止调用")),
		//	)
		//	return
		//}
		//
		//if len(data.Apis) < 1 {
		//	c.AbortWithError(core.Error(
		//		http.StatusBadRequest,
		//		code.AuthorizationError,
		//		code.Text(code.AuthorizationError)).WithError(errors.New(key + " 未进行接口授权")),
		//	)
		//	return
		//}
		//
		//if !whiteListPath[c.Path()] {
		//	// 验证 c.Method() + c.Path() 是否授权
		//	table := urltable.NewTable()
		//	for _, v := range data.Apis {
		//		_ = table.Append(v.Method + v.Api)
		//	}
		//
		//	if pattern, _ := table.Mapping(c.Method() + c.Path()); pattern == "" {
		//		c.AbortWithError(core.Error(
		//			http.StatusBadRequest,
		//			code.AuthorizationError,
		//			code.Text(code.AuthorizationError)).WithError(errors.New(c.Method() + c.Path() + " 未进行接口授权")),
		//		)
		//		return
		//	}
		//}
		//
		//ok, err := signature.New(key, data.Secret, configs.HeaderSignTokenTimeout).Verify(authorization, date, c.Path(), c.Method(), c.RequestInputParams())
		//if err != nil {
		//	c.AbortWithError(core.Error(
		//		http.StatusBadRequest,
		//		code.AuthorizationError,
		//		code.Text(code.AuthorizationError)).WithError(err),
		//	)
		//	return
		//}
		//
		//if !ok {
		//	c.AbortWithError(core.Error(
		//		http.StatusBadRequest,
		//		code.AuthorizationError,
		//		code.Text(code.AuthorizationError)).WithError(errors.New("Header 中 Authorization 信息错误")),
		//	)
		//	return
		//}
	}
}
